package com.yunwaikeji.gy_tool_library.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.yunwaikeji.gy_tool_library.model.GYAppModel;

import java.util.List;

public class GYAppUtils {
    public static GYAppModel getInfo(Context context) {
        GYAppModel appModel = new GYAppModel();
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            appModel.setVersionCode(packageInfo.versionCode);
            appModel.setVersionName(packageInfo.versionName);
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            appModel.setIcon(applicationInfo.loadIcon(packageManager));
            int labelRes = applicationInfo.labelRes;
            if (labelRes == 0) {
                CharSequence charSequence = packageManager.getApplicationLabel(applicationInfo);
                if (charSequence == null) {
                    appModel.setName(context.getPackageName());
                } else {
                    appModel.setName((String) charSequence);
                }
            } else {
                appModel.setName(context.getResources().getString(labelRes));
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return appModel;
    }

    /**
     * 获取apk包的信息：版本号，名称，图标等
     *
     * @param context
     * @param apkPath  apk包的绝对路径
     */
    public static GYAppModel getInfo(Context context, String apkPath) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        if (packageInfo != null) {
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            /* 必须加这两句，不然下面icon获取是default icon而不是应用包的icon */
            applicationInfo.sourceDir = apkPath;
            applicationInfo.publicSourceDir = apkPath;
            GYAppModel appModel = new GYAppModel();
            appModel.setName(packageManager.getApplicationLabel(applicationInfo).toString());
            appModel.setVersionCode(packageInfo.versionCode);
            appModel.setVersionName(packageInfo.versionName);
            appModel.setIcon(applicationInfo.loadIcon(packageManager));
            return appModel;
        }
        return null;
    }

    /**
     * 根据包名判断是否安装了应用
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean hasInstalled(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 检测是否安装微信
     *
     * @param context
     * @return
     */
//    public static boolean isInstalledWeiXin(Context context) {
//        IWXAPI wxApi = WXAPIFactory.createWXAPI(context, WXEntryActivity.APP_ID);
//        boolean bIsWXAppInstalledAndSupported = wxApi.isWXAppInstalled();
//        if (!bIsWXAppInstalledAndSupported) {
//            final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
//            List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
//            if (pinfo != null) {
//                for (int i = 0; i < pinfo.size(); i++) {
//                    String pn = pinfo.get(i).packageName;
//                    if (pn.equals("com.tencent.mm")) {
//                        return true;
//                    }
//                }
//            }
//            return false;
//        }
//        return true;
//    }
}
